<?php
if (!defined("MCTDESIGN")) die("# ");

if (isset($_POST['delete']) || isset($flag) && $flag == 1) {

	$total = isset($_POST["total"]) ? intval($_POST["total"]) : 0;

 	for($i=1;$i<=$total;$i++){

		$checkbox[$i] = isset($_POST["checkbox_$i"]) ? $_POST["checkbox_$i"] : '';

		if (!empty($checkbox[$i])) {

			$mysql->query("DELETE FROM ".$tb_fix."_yahoo WHERE yahoo_id='".$checkbox[$i]."'");

		}

	}

	$html_body = _html_trans("Đã xoá hỗ trợ thành công !");

	return false;

}
if (isset($_POST['delete_more_ll']) || isset($flag) && $flag == 1) {

	$total = isset($_POST["total"]) ? intval($_POST["total"]) : 0;

 	for($i=1;$i<=$total;$i++){

		$checkbox[$i] = isset($_POST["checkbox_$i"]) ? $_POST["checkbox_$i"] : '';

		if (!empty($checkbox[$i])) {

			$mysql->query("DELETE FROM ".$tb_fix."_lienhe WHERE mlh_id='".$checkbox[$i]."'");

		}

	}

	$html_body = _html_trans("Đã xoá liên hệ thành công !");

	return false;

}
if (isset($_POST['order'])) {

	$total = isset($_POST["total"]) ? intval($_POST["total"]) : 0;

    for ($i=1;$i<=$total;$i++)

      {

           $yahoo_id    = isset($_POST["order_id_$i"])    ? $_POST["order_id_$i"]    : 0;

           $yahoo_order = isset($_POST["order_$i"]) ? $_POST["order_$i"] : 0;

           if ( ($yahoo_id>0)&&($yahoo_order>0) )

           {		

				$mysql->query("UPDATE ".$tb_fix."_yahoo SET yahoo_order='$yahoo_order' WHERE yahoo_id='".$yahoo_id."'");

           }

    }

	$html_body = _html_trans("Đã cập nhật thứ tự hỗ trợ thành công !");

	return false;

}

if (isset($_POST['update'])) {
	$yahoo_nick = isset($_POST["yahoo_nick"]) ? fixquotes(stripslashes($_POST["yahoo_nick"])) : "";
	$yahoo_name = isset($_POST["yahoo_name"]) ? fixquotes(stripslashes($_POST["yahoo_name"])) : 0;
	$yahoo_description = isset($_POST["yahoo_description"]) ? fixquotes(stripslashes($_POST["yahoo_description"])) : "";
    $isPhoneSupport = (int)($_POST['isPhoneSupport']);
	if (empty($yahoo_nick) || empty($yahoo_name) || empty($yahoo_description)) {
		$html_body = _html_trans("Dữ liệu bạn nhập chưa đầy đủ !");
		return false;
	}
    if($isPhoneSupport){// sua phone
        $sql_update = " yahoo_phone = '$yahoo_nick',";
        $url = "Phone";
    }else{ //sua yahoo
        $sql_update = " yahoo_nick = '$yahoo_nick',";
        $url = "Yahoo";    
    }
    $anyDB->query("UPDATE ".$tb_fix."_yahoo SET $sql_update yahoo_description='$yahoo_description',yahoo_name='$yahoo_name' WHERE yahoo_id='".$id."'");  

	$html_body = _html_trans("Đã cập nhật thông tin hỗ trợ thành công !","?obj=Support&go=$url");
	return false;
}
if (isset($_POST['add'])) {
	$yahoo_nick = isset($_POST["yahoo_nick"]) ? fixquotes(stripslashes($_POST["yahoo_nick"])) : "";
	$yahoo_name = isset($_POST["yahoo_name"]) ? fixquotes(stripslashes($_POST["yahoo_name"])) : 0;
	$yahoo_description = isset($_POST["yahoo_description"]) ? fixquotes(stripslashes($_POST["yahoo_description"])) : "";
    $isPhoneSupport = (int)($_POST['isPhoneSupport']);
	if (empty($yahoo_nick) || empty($yahoo_name) || empty($yahoo_description)) {
		$html_body = _html_trans("Dữ liệu bạn nhập chưa đầy đủ !");
		return false;
	}
    if($isPhoneSupport){ // them so dien thoai
        $q = $anyDB->query("SELECT max(yahoo_order) AS maxorder FROM ".$tb_fix."_yahoo WHERE yahoo_ihome = 2 LIMIT 0,1");
    	$r = $anyDB->getNext($q);
    	$order = $r['maxorder']+1;
        $anyDB->query("INSERT INTO ".$tb_fix."_yahoo (yahoo_phone,yahoo_description ,yahoo_name ,yahoo_order, yahoo_ihome) VALUES ('$yahoo_nick','$yahoo_description','$yahoo_name','$order',2)");
	    $html_body = _html_trans("Đã thêm điện thoại hỗ trợ mới thành công !");
    }else{
        $q = $anyDB->query("SELECT max(yahoo_order) AS maxorder FROM ".$tb_fix."_yahoo WHERE yahoo_ihome = 1 LIMIT 0,1");
    	$r = $anyDB->getNext($q);
    	$order = $r['maxorder']+1;   
        $anyDB->query("INSERT INTO ".$tb_fix."_yahoo (yahoo_phone,yahoo_description ,yahoo_name ,yahoo_order,yahoo_ihome) VALUES ('$yahoo_nick','$yahoo_description','$yahoo_name','$order',1)");
	    $html_body = _html_trans("Đã thêm Nick Hỗ trợ mới thành công !");
    }
	return false;
}

function _html_manageYahoo()
{
	global $dir_tpl, $tb_fix,$temp,$anyDB;
	$temp_body = $temp;
    $sql = "SELECT * FROM ".$tb_fix."_yahoo WHERE yahoo_ihome=1 ORDER BY yahoo_order ASC";
    $q = $anyDB->query($sql);
    $row = $anyDB->getAll($q);
    $temp_body->assign("yahoo",$row);    
    $temp_body->assign("breakcum",breakcum("Quản lý hỗ trợ trực tuyến  "));
	$_html = $temp_body->fetch( '_adm_yahoo.mct' );
	return $_html;
}
function _html_editYahoo($_id="")
{
	global $dir_tpl, $mysql, $tb_fix,$admin_id,$admin_permissions,$temp;
	$q = $mysql->query("SELECT * FROM ".$tb_fix."_yahoo WHERE yahoo_id='$_id' LIMIT 1");
	$t = $mysql->num_rows($q);
	if ($t>0) {
		$r = $mysql->fetch_array($q);
		$temp_body = $temp;
		$temp_body->assign(array(
								"yahoo_nick" => "".$r["yahoo_nick"]."",                                
                                "yahoo_phone" => "".$r["yahoo_phone"]."",
                                "yahoo_name" => "".$r["yahoo_name"]."",                                
								"yahoo_description" => "".$r["yahoo_description"]."",
								"yahoo_id" => "".$r["yahoo_id"].""
							)
						);
        $temp_body->assign("breakcum",breakcum("Quản lý hỗ trợ trực tuyến  "));
		$_html = $temp_body->fetch( '_adm_yahoo_edit.mct' );
	}
	return $_html;
}

function _html_managePhone()
{
	global $dir_tpl, $tb_fix,$temp,$anyDB;
	$temp_body = $temp;
    $sql = "SELECT * FROM ".$tb_fix."_yahoo WHERE yahoo_ihome=2 ORDER BY yahoo_order ASC";
    $q = $anyDB->query($sql);
    $row = $anyDB->getAll($q);
    $temp_body->assign("yahoo",$row);    
    $temp_body->assign("breakcum",breakcum("Quản lý hỗ trợ trực tuyến  "));
	$_html = $temp_body->fetch( '_adm_phone.mct' );
	return $_html;
}
function _html_manageContact(){
    global $dir_tpl, $tb_fix,$admin_id,$admin_permissions,$anyDB,$temp;
    $temp->assign("breakcum",breakcum("Danh sách liên hệ"));
 // phân trang
    $pagegroup_size = 10;
    $page=_POST('p');
    if (!$page||$page==0){$page=1;}
    $limit=_POST('limit');
    if($limit==''||$limit>5) {$limit=15;}
    $offset=($page-1)*$limit;
    $li="limit $offset,$limit";
    $sql = "select count(mlh_id) as total_r from ".$tb_fix."_lienhe";         
    $q = $anyDB->query($sql);
    $arr = $anyDB->getNext($q);
    $totalRecord = $arr['total_r']; // tong so ban ghi     
    $sql = "select * from ".$tb_fix."_lienhe order by mlh_id desc $li";
    $q = $anyDB->query($sql);
    $r = $anyDB->getAll($q);  
    foreach($r as $k =>$v){
        $r[$k]['mlh_thoigian'] = gmdates($v['mlh_thoigian']);
    }
    $temp->assign("rows",$r);
    $pages = pagenavigator($page,$totalRecord,$limit,$pagegroup_size,"admin.php?obj=Support&go=Contact");
    $temp->assign('pages',$pages);
    $temp->assign('dir_tpl',$dir_tpl);
	$_html = $temp->fetch( '_adm_contact.mct' );
	return $_html; 
}
function _html_editContact($id){
    global $dir_tpl, $tb_fix,$admin_id,$admin_permissions,$temp,$anyDB;
    $id = (int)$id;
    $sql = "update ".$tb_fix."_lienhe set mlh_status = 1 where mlh_id = $id";
    $anyDB->query($sql);
  // info contact
    $sql = "select * from ".$tb_fix."_lienhe
             where mlh_id = '$id' limit 0,1";
             //echo $sql;
    $q = $anyDB->query($sql);
    $t = $anyDB->numRows($q);
    if($t > 0){
        $r = $anyDB->getNext($q);
        //test($r,1);
        $temp->assign("dir_tpl",$dir_tpl);        
        $temp->assign("row",$r); 
        $temp->assign("breakcum",breakcum("Thông tin liên hệ"));
    }else{
        $html_body =_html_trans(" Không tìm thấy thông tin liên hệ!");
        return false;   
    }    
    $temp->assign('base_url',$site_path);
    $temp->assign("base_tpl",$dir_tpl); 
	$_html = $temp->fetch( '_adm_contact_view.mct' );
	return $_html; 
}
//Navigation
$go = $_GET['go'];
switch($go){
    case "Yahoo":
        if($act == "edit"){
            $html_body = _html_editYahoo($id);        
        }else{            
            $html_body = _html_manageYahoo();     
        }       
       break;
    case "Phone":
        if($act == "edit"){
            $html_body = _html_editYahoo($id);        
        }else{
            $html_body = _html_managePhone();     
        }       
       break; 
    case "Contact":
        if($act == "edit"){
            $html_body = _html_editContact($id);        
        }else{
            $html_body = _html_manageContact();     
        }       
       break;  
    default:
             $html_body = _html_manageYahoo();
             break;
}
?>